package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.instance.entity.Attendance;
import com.a01_2021.thousandstimes.instance.service.IAttendanceService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/attendance")
public class AttendanceController {

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @Resource
    IAttendanceService attendanceService;

    /**
     * 签到
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('attendance-checkIn')")
    @DS("#header.instanceid")
    @PostMapping("checkIn")
    public AjaxResponse checkIn(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Attendance attendance = attendanceService.checkIn(userId);
        return AjaxResponse.success(attendance);
    }

    /**
     * 签退
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('attendance-checkIn')")
    @DS("#header.instanceid")
    @PostMapping("checkOut")
    public AjaxResponse checkOut(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        Attendance attendance = attendanceService.checkOut(userId);
        return AjaxResponse.success(attendance);
    }

    /**
     * 查询用户签到签退记录
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('attendance-getAttendance')")
    @DS("#header.instanceid")
    @PostMapping("getAttendance")
    public AjaxResponse getAttendance(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token) {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        List<Attendance> attendanceList = attendanceService.getAttendance(userId);
        return AjaxResponse.success(attendanceList);
    }

}
